<!DOCTYPE html>
<html>

<head>

    <!--java script for interacting with the RESTful API-->
    <script type="text/javascript" src="/site_media/jquery/js/jquery-1.6.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/site_media/stylesheet.css" />
    <link type="text/css" href="/site_media/jquery/css/aristo/jquery-ui-1.8.7.custom.css" rel="Stylesheet" />
    <script type="text/javascript" src="/site_media/jquery/js/jquery-ui-1.8.13.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/site_media/report.css" media="screen" />

    <!--page style-->
    <style type='text/css'>
        #formwrap {
            line-height: 2em;
            background: #eef;
            margin: 10px;
            padding: 10px;
            height: 130px;
        }
        body {
            font-family: "Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;
            font-size: 12px;
        }
    </style>

    <script type='text/javascript'>
        //grab the global parameter value for genome_file via the RESTful API
        pluginGET = "";

        $(document).ready(function() {

            $.ajaxSetup({
                async: false
            });
            // get the reference for the current report - TS_result is defined in run verison of this script
            var reportUrl = '/rundb/api/v1/results/' + TB_result + '/?format=json&noplugin=True';
            var referenceID;
            $.get(reportUrl, function(data) {
                referenceID = data.reference;
            });

            $.ajax({

                url: "/rundb/api/v1/referencegenome/?limit=0&enabled=true&index_version=" + TB_TMAP + "&format=jsonp",
                success: function(data) {

                    $("#agenome").html("");
                    $("#bgenome").html("");

                    var items = "";
                    var item0 = "";
                    jQuery.each(data.objects,
                        function(i, element) {
                            if (element.short_name == referenceID) {
                                item0 = "<option value='" + element.reference_path + "/" + element.short_name + ".fasta'>" + element.short_name + "</option>";
                            } else {
                                items += "<option value='" + element.reference_path + "/" + element.short_name + ".fasta'>" + element.short_name + "</option>";
                            }

                        });

                    $("#agenome").html("<option>None</option>" + item0 + items);
                    $("#bgenome").html(item0 + "<option>None</option>" + items);

                },
                type: "GET",
                dataType: "jsonp"

            });
        });


        $(function() {

            $.fn.serializeObject = function() {
                var o = {};
                var a = this.serializeArray();
                $.each(a,
                    function() {
                        if (o[this.name] != null) {
                            if (!o[this.name].push) {
                                o[this.name] = [o[this.name]];
                            }
                            o[this.name].push(this.value || '');
                        } else {
                            o[this.name] = this.value || '';
                        }
                    });
                return o;
            };

            function getSpadesOptions() {
                var result = '';
                if ($('#spadesOptions').val() != 'custom') {
                    result = $('#spadesOptions').val();
                } else {
                    result = "-k " + $('#spadesK').val() + " " + $('#spadesMode').val();
                }

                if (!($('#runIonHammer').attr('checked'))) {
                    result += ' --only-assembler';
                }
                return result;
            }

            $(function() {

                $('#postbutton').click(function() {
                    obj = $('#pluginconfig').serializeObject();

                    obj['spadesOptions'] = getSpadesOptions();
                    pluginAPIJSON = {
                        "plugin": [TB_plugin.fields.name],
                        "pluginconfig": obj
                    };
                    pluginAPIJSON = JSON.stringify(pluginAPIJSON);

                    pluginURL = "/rundb/api/v1/results/" + TB_result + "/plugin/";

                    $.ajax({
                        type: 'POST',
                        url: pluginURL,
                        contentType: "application/json; charset=utf-8",
                        data: pluginAPIJSON,
                        dataType: "json",
                        success: function() {
                            parent.$.fn.colorbox.close();
                        }
                    });
                });
            })
        });

        $(function() {
            $('#spadesOptions').change(function() {
                if ($(this).val() == 'custom') {
                    $("#customSpadesOptions").show('slow');
                } else {
                    $("#customSpadesOptions").hide('slow');
                }
            });
        });
    </script>
    <script type='text/javascript'>
        $(function() {
            $("#postbutton").button();
            $("#postbutton").css("margin", 20);
        });
    </script>

    <script type='text/javascript'>
        function selectrun() {
            var myselect = document.getElementById('runselect')
            for (var i = 0; i < myselect.options.length; i++) {
                if (myselect.options[i].selected) {
                    document.getElementById(myselect.options[i].value).style.display = "";
                } else {
                    document.getElementById(myselect.options[i].value).style.display = "none"
                }
            }
        }
    </script>

</head>

<!--build the html page for what the user sees-->

<body>
    <center>
        <input id="postbutton" type="submit" value="Submit the Plugin"></input>
    </center>
    <br>
    <form id="pluginconfig">
        <div id="adv_parent">
            <h3 id="adv" align=right>Advanced Settings:&nbsp &nbsp &nbsp &nbsp<a href="javascript:;" class="expandCollapseButton" title="Collapse Section"></a></h3>
            <div>
                <fieldset id=advanced_settings>

                    <center>
                        <table align="center">
                            <tr>
                                <td align=right>QUAST Reference (Change to 'None' to disable QUAST):</td>
                                <td align=left>
                                    <select name="bgenome" id="bgenome"></select>
                                </td>
                            </tr>
                            <tr>
                                <td align=right>Fraction of reads to use:</td>
                                <td align=left>
                                    <select name="fraction_of_reads">
                                        <option value="1" selected="selected">100%</option>
                                        <option value=".9">90%</option>
                                        <option value=".8">80%
                                            <option value=".7">70%</option>
                                            <option value=".6">60%</option>
                                            <option value=".5">50%</option>
                                            <option value=".4">40%</option>
                                            <option value=".3">30%</option>
                                            <option value=".2">20%</option>
                                            <option value=".1">10%</option>
                                        </option>
                                        <option value=".05">5%</option>
                                        <option value=".01">1%</option>
                                        <option value=".001">.1%</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td align=right>Only process barcodes:</td>
                                <td>
                                    <input name="only_barcodes" id="only_barcodes" value="">(comma-delimited,no white-space)</td>
                            </tr>
                            <tr>
                                <td align=right>Skip barcodes with fewer than</td>
                                <td>
                                    <input name="min_reads" id="min_reads" value="500">reads</td>
                            </tr>
                            <tr>
                                <td align=right>RAM to allocate:</td>
                                <td align=left>
                                    <select name="RAM" id="RAM">
                                        <option value="8G">8Gb</option>
                                        <option value="16G">16Gb</option>
                                        <option value="24G">24Gb</option>
                                        <option value="32G" selected="selected">32Gb</option>
                                        <option value="40G">40Gb</option>
                                        <option value="46G">46Gb (max)</option>
                                    </select>
                                </td>
                            </tr>
                        </table>
                    </center>

                    <input type=checkbox name=runMira value=1><b>Assembly by MIRA:</b>
                    <center>
                        <table align="center">
                            <tr>
                                <td align=right>Assisting Reference (leave as 'None' for non-assisted assembly):</td>
                                <td align=left>
                                    <select name="agenome" id="agenome"></select>
                                </td>
                            </tr>
                            <tr>
                                <td align=right>MIRA Version:</td>
                                <td align=left>
                                    <select name="miraversion">
                                        <option value="4.0" selected="selected">4.0</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td align=right>Assembly Type:</td>
                                <td align=left>
                                    <select name="type">
                                        <option value="draft">Draft</option>
                                        <option selected="selected" value="accurate">Accurate</option>
                                    </select>
                                </td>
                            </tr>
                        </table>
                    </center>

                    <input type=checkbox checked=checked name=runSpades value=1><b>Assembly by SPAdes</b> (a faster and more conservative assembler, giving shorter N50 but less mis-assemblies):
                    <center>
                        <table align="center">
                            <tr>
                                <td align=right>SPAdes Version:</td>
                                <td align=left>
                                    <select name="spadesversion">
                                        <option value="2.5.1">2.5.1</option>
                                        <option value="3.0.0" selected="selected">3.0.0</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td align=right>Assembly settings:</td>
                                <td align=left>
                                    <select id="spadesOptions">
                                        <option value="-k 21,33,55,77,99" selected="selected">Uniform coverage</option>
                                        <option value="-k 21,33,55,77,99 --sc">Non-uniform coverage</option>
                                        <option value="-k 21,33,55 --sc">Highly non-uniform coverage</option>
                                        <option value="custom">Custom...</option>
                                    </select>
                                    <div style="display: none" id="customSpadesOptions">
                                        K:
                                        <input type=text id="spadesK" value="21,33,55,77,99">(comma-delimited, no spaces)
                                        <br/>Mode:
                                        <select id="spadesMode">
                                            <option value=" " selected="selected">Multi-cell</option>
                                            <option value="--sc">Single-cell</option>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td colspan=2 style='text-align:center'>
                                    <input type=checkbox id="runIonHammer" checked=checked value=1>Run read correction before doing assembly</td>
                            </tr>
                        </table>
                    </center>
                    <input type=checkbox name=quastOnly value=1><b>Skip assembly if previous results exist</b>
                </fieldset>
            </div>
        </div>
        </div>
    </form>

    <script>
        $('.expandCollapseButton').toggle(function() {
            if ($(this).attr('title') == 'Collapse Section') {
                $(this).css('background-position', 'right top');
                $(this).attr('title', 'Expand Section');
            } else {
                $(this).css('background-position', 'left top');
                $(this).attr('title', 'Collapse Section');
            }
        }, function() {
            if ($(this).attr('title') == 'Expand Section') {
                $(this).css('background-position', 'left top');
                $(this).attr('title', 'Collapse Section');
            } else {
                $(this).css('background-position', 'right top');
                $(this).attr('title', 'Expand Section');
            }
        });

        $('.expandCollapseButton').click(function(event) {
            $(event.target).parent().parent().toggleClass('small');
            $(event.target).parent().next().slideToggle();
        });

        $('#adv .expandCollapseButton').css('background-position', 'right top');
        $('#adv .expandCollapseButton').attr('title', 'Expand Section');
        $('#adv').parent().toggleClass('small');
        $('#adv').next().toggle();
    </script>

    <div id="json_result"></div>

</body>

</html>
